#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import requests
from bs4 import BeautifulSoup


def usingBs4(floderName):
    with open(f"resource\{floderName}\{floderName}.txt", 'rb+') as f:
        htmlText = f.read()
    # 标准库解析HTML
    bs = BeautifulSoup(htmlText, 'html.parser')
    print(bs.title)
    print(bs.title.text)
    for item in bs.find_all('a'):
        print(item.get('href'))
    # lxml HTML解析器
    soup = BeautifulSoup(htmlText, 'lxml')
    print(soup.title)
    print(soup.title.name)
    print(soup.title.string)
    for item in soup.find_all('a'):
        print(item.get('href'))
    # print(soup.get_text())
    # CSS选择器搜索
    # 通过标签查找
    print(soup.select('title'))
    # 通过类名查找
    print(soup.select('.toindex'))
    # 通过id名查找
    print(soup.select('#m'))
    print(soup.select('#m,title'))
    print(soup.select('a[class="toindex"]'))


def openFile(htmlText, floderName):
    if floderName == '':
        floderName = 'items'
    # Judge whether the directory exists. If it does not exist, create a new directory
    savePath = os.path.abspath(os.path.join(
        os.getcwd(), f'resource\{floderName}'))
    if not os.path.exists(savePath):
        os.makedirs(savePath)
    with open(f"resource\{floderName}\{floderName}.txt", 'ab') as f:
        f.write(htmlText)
    usingBs4(floderName)


def input_str():
    string = input()
    return string


def main():
    print("Enter a website：", end='')
    url = input_str()
    if url == '':
        url = 'https://www.baidu.com'
    print('Enter a floderName：', end='')
    floderName = input_str()
    from fake_useragent import UserAgent
    ua = UserAgent()
    # headers = {'User-Agent': ua.random}  # 随机生成浏览器User-Agent
    headers = {'User-Agent': ua.chrome}  # 指定浏览器User-Agent
    response = requests.get(url, headers=headers)
    # print(response.status_code)
    htmlText = response.content
    openFile(htmlText, floderName)


if __name__ == '__main__':
    main()
    print('done')
